from flask import request
from flask_restful import Resource
from exts import api, db
from utils.auth import auth_service
from flask import jsonify
from models.user import AdminUser, RoleType
from services.audit_service import audit_service


class audit_api(Resource):
    @auth_service().require_role([RoleType.SUPER, RoleType.AUDIT])
    @auth_service().verify_token
    def get(self, audit_record_id=None):
        try:
            user_id = auth_service().get_user_id_from_token()
            user_info = auth_service().get_user_info_from_token()
            role_type = RoleType(user_info["role_type"])
            if audit_record_id:
                # 获取单条审计记录
                audit_record = audit_service().get_audit_record(audit_record_id, user_id, role_type)
                return {
                    "message": "查询成功",
                    "audit_record": audit_record,
                    "code": 200  # 添加 code 信息
                }
            else:
                # 获取所有审计记录
                audit_records = audit_service().get_audit_records(user_id, role_type)
                return {
                    "message": "查询成功",
                    "audit_records": audit_records,
                    "code": 200  # 添加 code 信息
                }
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role([RoleType.SUPER, RoleType.AUDIT])
    @auth_service().verify_token
    def post(self):
        try:
            post_json = request.get_json()
            user_id = auth_service().get_user_id_from_token()  # 获取用户 ID
            user_info = auth_service().get_user_info_from_token()
            role_type = RoleType(user_info["role_type"])
            inventory_id = post_json.get("inventory_id")
            status = post_json.get("status")
            comment = post_json.get("comment")

            if not inventory_id or not status:
                return {"message": "库存ID和审核状态不能为空", "code": 400}, 400

            result = audit_service().update_inventory_audit(
                inventory_id, user_id, role_type, status, comment
            )
            return {
                "message": "审核成功",
                "result": result,
                "code": 200  # 添加 code 信息
            }
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role([RoleType.SUPER, RoleType.AUDIT])
    @auth_service().verify_token
    def put(self):
        try:
            put_json = request.get_json()
            user_id = auth_service().get_user_id_from_token()  # 获取用户 ID
            user_info = auth_service().get_user_info_from_token()
            role_type = RoleType(user_info["role_type"])
            audit_record_id = put_json.get("id")
            status = put_json.get("status")
            comment = put_json.get("comment")


            if not audit_record_id or not status:
                return {"message": "审计记录ID和审核状态不能为空", "code": 400}, 400

            result = audit_service().update_audit_record(
                audit_record_id, user_id, role_type, status, comment
            )
            return {
                "message": "审计记录修改成功",
                "result": result,
                "code": 200  # 添加 code 信息
            }
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def delete(self):
        try:
            delete_json = request.get_json()
            user_info = auth_service().get_user_info_from_token()
            role_type = RoleType(user_info["role_type"])
            audit_record_id = delete_json.get("id")

            if not audit_record_id:
                return {"message": "审计记录ID不能为空", "code": 400}, 400

            result = audit_service().delete_audit_record(audit_record_id, role_type)
            return {**result, "code": 200}  # 添加 code 信息
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()


api.add_resource(audit_api, "/api/audit", "/api/audit/<int:audit_record_id>")        
